package com.quartz.task;

import com.quartz.properties.CronProperties;
import com.quartz.service.HelloSevice;
import lombok.extern.slf4j.Slf4j;
import org.quartz.DisallowConcurrentExecution;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @描述:
 * @类名:
 * @作者: yangshuang
 * @创建时间: 2017/8/17 19:17
 * @版本: v1.0
 */
@Slf4j
@Service
@DisallowConcurrentExecution//同步方式运行
public class MyTask extends BaseTask {

    /**
     * 注入不进来
     * Quartz中的job是由 Quartz动态创建的
     * job不是一个spring bean
     * 如何将job交给spring进行管理
     * 使用AutowireCapableBeanFactory进行
     */
    @Resource
    private HelloSevice helloSevice;

    @Autowired
    private CronProperties cronProperties;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {

            /*log.info("MyTtask jobScheduler={}", jobExecutionContext.getScheduler().getSchedulerName());
            log.info("job trigger = {}.",jobExecutionContext.getTrigger());*/
       /* ScheduleJob scheduleJob = (ScheduleJob)jobExecutionContext.getMergedJobDataMap().get("scheduleJob");
        System.out.println("任务名称 = [" + scheduleJob.getJobName() + "]");*/
        System.out.println("MyTask" + new Date());
        helloSevice.hello();

    }

    @Override
    public String getCronExpression() {
        return cronProperties.getCronExpression();
    }
}
